package com.gxks.mall_admin.controller;

import com.gxks.commons.entity.Dictionary;
import com.gxks.commons.entity.Manager;
import com.gxks.commons.entity.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ManagerController {

    private static final String MANAGER_URL = "http://localhost:8002/";

    private static final String DICTIONARY_URL = "http://localhost:8001/";

    @Autowired
    private RestTemplate restTemplate;

    @ResponseBody
    @PutMapping("/admin/manager/editState/{state}")
    public ResponseData editState(Long[] managerId ,
                                  @PathVariable Integer state){
        restTemplate.put(MANAGER_URL + "manager/manager/editState/" + state , managerId);
        return new ResponseData(200 , "状态修改成功！" , null);
    }

    @ResponseBody
    @DeleteMapping("/admin/manager")
    public ResponseData delSome(Long[] managerId){
        ResponseData result =
                restTemplate.postForObject(MANAGER_URL + "manager/manager/delsome",
                        managerId, ResponseData.class);
        return result;
    }

    @ResponseBody
    @PutMapping("/admin/manager")
    public ResponseData<Manager> editSave(Manager manager){
        try {
            restTemplate.put(MANAGER_URL + "manager/manager",manager);
            return new ResponseData<>(200 , "edit ok !" ,manager);
        }catch (Exception e){
            return new ResponseData<>(500 , "edit fail !" ,manager);
        }
    }

    @GetMapping("/admin/manager/{id}")
    public String getManagerById(@PathVariable Long id , Model model){
        ResponseData manager = restTemplate.getForObject(MANAGER_URL + "manager/manager/" + id,
                ResponseData.class);
        model.addAttribute("manager" , manager);
        //查询和管理员状态相关的字典数据并放入请求域
        ResponseData<List<Dictionary>> result =
                restTemplate.getForObject(DICTIONARY_URL + "dic/dictionary?dicType=user_state",
                        ResponseData.class);
        model.addAttribute("result",result);
        return "manager/managerEdit";
    }

    @ResponseBody
    @PostMapping("/admin/manager")
    public ResponseData<Manager> addSave(Manager manager){
        ResponseData result = restTemplate.postForObject(MANAGER_URL + "manager/manager",
                manager, ResponseData.class);
        return result;
    }

    @GetMapping("/admin/manager/add")
    public String add(Model model){
        //查询和管理员状态相关的字典数据并放入请求域
        ResponseData<List<Dictionary>> result =
                restTemplate.getForObject(DICTIONARY_URL + "dic/dictionary?dicType=user_state",
                ResponseData.class);
        model.addAttribute("result",result);
        return "manager/managerAdd";
    }

    @GetMapping("/admin/manager")
    public String managerList(Model model ,
                              @RequestParam(value = "keywords" , defaultValue = "") String keywords,
                              @RequestParam(value = "pageNum" , defaultValue = "1") Integer pageNum,
                              @RequestParam(value = "pageSize" , defaultValue = "3") Integer pageSize){
        ResponseData result =
                restTemplate.getForObject(MANAGER_URL + "manager/manager?keywords=" + keywords + "&pageNum=" + pageNum + "&pageSize=" +pageSize,
                        ResponseData.class);
        model.addAttribute("result",result);
        model.addAttribute("keywords",keywords);
        return "manager/managerList";
    }

    @ResponseBody
    @DeleteMapping("/admin/manager/{id}")
    public ResponseData deleteById(@PathVariable Long id){
        restTemplate.delete(MANAGER_URL + "manager/manager/" + id);
        return new ResponseData(200 , "delete ok !" , id);
    }

}
